using KinonekoSoftware.CodariaDB.Documents.Templates;
// ReSharper disable RedundantArgumentDefaultValue

namespace KinonekoSoftware.Studio.Pages.Theories
{

    public sealed class WVCTemplatePropertyEditorViewModel : BooleanDialog
    {
        private CompositeItem _selected;

        internal enum CompositeMode
        {
            Property,
            Paragraph,
            ToggleSwitch,
        }

        private bool _isProperty;
        private bool _isParagraph;
        private bool _isToggleSwitch;


        public WVCTemplatePropertyEditorViewModel()
        {
            _isProperty = true;
            AddPropertyCommand       = CreateCommand(DoAddPropertyCommand);
            ShiftUpPropertyCommand   = CreateCommand<CompositeItem>(DoShiftUpPropertyCommand);
            ShiftDownPropertyCommand = CreateCommand<CompositeItem>(DoShiftDownPropertyCommand);
            RemovePropertyCommand    = CreateCommand<CompositeItem>(DoRemovePropertyCommand);

        }

        public static async Task<bool> Edit(ObservableCollection<CompositeItem> properties)
        {
            var context = new DialogContext();

            context.Set(ContextBase.Key_Value, properties);
            return await Question(new WVCTemplatePropertyEditorViewModel(), context);
        }

        private void DoAddPropertyCommand()
        {
            CompositeItem item = Mode switch
            {
                CompositeMode.Paragraph    => InstanceTemplateGenerator.Paragraph(TextSR.Parameter_Untitled, string.Empty),
                CompositeMode.ToggleSwitch => InstanceTemplateGenerator.ToggleSwitch(TextSR.Parameter_Untitled, string.Empty),
                _                          => InstanceTemplateGenerator.Property(TextSR.Parameter_Untitled, string.Empty),
            };


            Properties.Add(item);
        }


        private async void DoShiftUpPropertyCommand(CompositeItem target)
        {
            if (target is null)
            {
                return;
            }


            Properties.ShiftUp(target);
        }

        private async void DoShiftDownPropertyCommand(CompositeItem target)
        {
            if (target is null)
            {
                return;
            }


            Properties.ShiftDown(target);
        }

        private async void DoRemovePropertyCommand(CompositeItem target)
        {
            if (target is null)
            {
                return;
            }

            if (!target.IsEditable)
            {
                await Danger(TitleSR.DangerOperation, TextSR.Remove_Item_Failed_Locked);
                return;
            }

            if (!target.IsRemovable)
            {
                await Danger(TitleSR.DangerOperation, TextSR.Remove_Item_Failed_Locked);
                return;
            }

            Properties.Remove(target);
        }

        protected override void OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Value))
            {
                Properties = context.GetObject<ObservableCollection<CompositeItem>>(ContextBase.Key_Value);
            }

            base.OnReceive(context);
        }


        internal CompositeMode Mode { get; set; }

        /// <summary>
        /// 获取或设置 <see cref="IsToggleSwitch"/> 属性。
        /// </summary>
        public bool IsToggleSwitch
        {
            get => _isToggleSwitch;
            set
            {
                if (value)
                {
                    Mode = CompositeMode.ToggleSwitch;
                }
                

                _isProperty = _isParagraph = false;
                _isToggleSwitch = value;
                RaiseUpdated(nameof(IsParagraph));
                RaiseUpdated(nameof(IsProperty));
                RaiseUpdated(nameof(IsToggleSwitch));
            }
        }
        /// <summary>
        /// 获取或设置 <see cref="IsParagraph"/> 属性。
        /// </summary>
        public bool IsParagraph
        {
            get => _isParagraph;
            set
            {
                if (value)
                {
                    Mode = CompositeMode.Paragraph;
                }

                _isProperty = _isToggleSwitch = false;
                _isParagraph = value;
                RaiseUpdated(nameof(IsParagraph));
                RaiseUpdated(nameof(IsProperty));
                RaiseUpdated(nameof(IsToggleSwitch));
            }
        }
        /// <summary>
        /// 获取或设置 <see cref="IsProperty"/> 属性。
        /// </summary>
        public bool IsProperty
        {
            get => _isProperty;
            set
            {
                
                if (value)
                {
                    Mode = CompositeMode.Property;
                }               

                _isParagraph = _isToggleSwitch = false;
                _isProperty = value;
                RaiseUpdated(nameof(IsParagraph));
                RaiseUpdated(nameof(IsProperty));
                RaiseUpdated(nameof(IsToggleSwitch));
            }
        }

        public CompositeItem Selected
        {
            get => _selected;
            set => SetValue(ref _selected, value);
        }

        public ObservableCollection<CompositeItem> Properties               { get; private set; }
        public ICommandEX                          AddPropertyCommand       { get; }
        public ICommandEX                          ShiftUpPropertyCommand   { get; }
        public ICommandEX                          ShiftDownPropertyCommand { get; }
        public ICommandEX                          RemovePropertyCommand    { get; }

    }
}